package com.dao.green.cn.databinding01.uis.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.dao.green.cn.databinding01.R;

/**
 * Created by Ant on 2017/7/4  21:29
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class CustomerTextView extends View {
    private String mText;
    private int mTextColor;
    private int mTextSize;

    private Paint mPaint;
    private Rect mRect;


    public CustomerTextView(Context context) {
        this(context, null);
    }

    public CustomerTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray type = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomerTextView, defStyleAttr, 0);
        int typeCount = type.getIndexCount();
        int attr = 0;
        for (int i = 0; i < typeCount; i++) {
            attr = type.getIndex(i);
            switch (attr) {
                case R.styleable.CustomerTextView_text:
                    mText = type.getString(attr);
                    break;
                case R.styleable.CustomerTextView_textColor:
                    mTextColor = type.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomerTextView_textSize:
                    mTextSize = type.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        type.recycle();

        //
        mPaint = new Paint();
        mPaint.setTextScaleX(mTextSize);
        mPaint.setColor(mTextColor);
        mRect = new Rect();
        mPaint.getTextBounds(mText, 0, mText.length(), mRect);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(mTextColor);
        canvas.drawText(mText, getWidth() / 2 - mRect.width() / 2, getHeight() / 2 - mRect.height() / 2, mPaint);
    }
}
